import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JOptionPane;
import java.util.Date;
import java.text.SimpleDateFormat;
import java.text.ParseException;

// Calculates the GCD of three integers
public class GUI extends JFrame implements ActionListener
{
  
  //JTextField for info driver given
  private final JTextField driverNoTextField = new JTextField(20);
  private final JTextField startDateTextField = new JTextField(20);
  private final JTextField endDateTextField = new JTextField(20);


  //JTextField for the result
  private final JLabel DriverIDTextField = new JLabel("");
  private final JLabel DriverNameTextField = new JLabel("");
  private final JLabel DriverHoliTakenTextField = new JLabel("");
  private final JLabel stDateConfirmed = new JLabel("Not done");
  private final JLabel edDateConfirmed = new JLabel("Not done");
  private final JLabel noDaysConfirmed = new JLabel("");
  private final JLabel DriverHoliTakenA = new JLabel("");
  
  //private final JTextField idConfirmed = new JTextField(20);
  
  // Button
  JButton computeJButton;
  JButton holiApplyJButton;
  JButton holiConfirmJButton;
  JButton routerViewer;
  // Global Variable
  int DriverHoliTaken = 0;
  public static int driverId = 0; 
  
  
  // GUI Construct
  public GUI()
  {
    setTitle("Driver's info");
    Container contents = getContentPane();
    contents.setLayout(new BorderLayout());
    // The first Panel for collect driver's info
    JPanel driverInfo = new JPanel(new GridLayout(8,1));
    driverInfo.add(new JLabel("DriverNo"));
    driverInfo.add(driverNoTextField);
    driverInfo.add(new JLabel(""));
    computeJButton = new JButton("Submit");
    driverInfo.add(computeJButton);
    computeJButton.addActionListener(this);
    driverInfo.add(new JLabel(""));
    driverInfo.add(new JLabel(""));
    driverInfo.add(new JLabel("Driver's ID"));
    driverInfo.add(DriverIDTextField);
    driverInfo.add(new JLabel("Driver's Name"));
    driverInfo.add(DriverNameTextField);
    driverInfo.add(new JLabel("Holiday's Taken"));
    driverInfo.add(DriverHoliTakenTextField);
    driverInfo.add(new JLabel(""));
    driverInfo.add(new JLabel(""));
    {
			routerViewer = new JButton();
			driverInfo.add(routerViewer);
			routerViewer.setText("View router");
			routerViewer.addActionListener(this);
		}
    holiApplyJButton = new JButton("Apply");
    driverInfo.add(holiApplyJButton);
    holiApplyJButton.setText("Holiday apply");
    holiApplyJButton.addActionListener(this);
    
    contents.add(driverInfo, BorderLayout.NORTH);
  
    setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    pack();
  } // GCD
  
  //GUI Construct
  public GUI(int id)
  {
    setTitle("Holiday apply");
    Container contents = getContentPane();
    contents.setLayout(new BorderLayout());
    driverId = id;
    JPanel holidayApply = new JPanel(new GridLayout(7,1));
    holidayApply.add(new JLabel("Start date(DD/MM/YYYY)"));
    holidayApply.add(startDateTextField);
    holidayApply.add(new JLabel("End date(DD/MM/YYYY)"));
    holidayApply.add(endDateTextField);
    holidayApply.add(new JLabel(""));
    holiConfirmJButton = new JButton("Confirm");
    holidayApply.add(holiConfirmJButton);
    holidayApply.add(new JLabel("Start date(DD/MM/YYYY)"));
    holidayApply.add(stDateConfirmed);
    holidayApply.add(new JLabel("End date(DD/MM/YYYY)"));
    holidayApply.add(edDateConfirmed);
    holidayApply.add(new JLabel("Number of requested day:"));
    holidayApply.add(noDaysConfirmed);
    holidayApply.add(new JLabel("Holidays taken so far:"));
    holidayApply.add(DriverHoliTakenA);
    holiConfirmJButton.addActionListener(this);
    
    contents.add(holidayApply, BorderLayout.NORTH);
    
    setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    pack();
  } // GUI

  // Act upon the button being pressed
  public void actionPerformed(ActionEvent event) 
  {
	  // Action to get the driver's info from database by driverNo.
    if (event.getSource() == computeJButton)
    {  
      String DriverNo = driverNoTextField.getText();
      
      if (DriverInfo.findDriver(DriverNo)==0)
      {
          Object[] options = {"OK"};
          JOptionPane.showOptionDialog(null,"invaild Driver Number","Warning",
                                       JOptionPane.DEFAULT_OPTION, 
        		                       JOptionPane.WARNING_MESSAGE,null,options,options[0]);
      }
      else
      {	  
      driverId = DriverInfo.findDriver(DriverNo);
      Driver driver = Driver.createDriver(driverId);
      String DriverName = driver.getName();
      DriverHoliTaken = driver.getHolidaysTaken();
      DriverIDTextField.setText("" + driverId);
      DriverNameTextField.setText("" + DriverName);
      DriverHoliTakenTextField.setText("" + DriverHoliTaken);
      }
      }
    // if days taken less than 25, apply panel displays
    if (driverId != 0 && DriverHoliTaken < 25 && event.getSource() == holiApplyJButton)
    {
   	  GUI xxx = new GUI(driverId);
   	  xxx.setVisible(true);
      
    }
    if (driverId != 0 && event.getSource() == routerViewer)
		{
			new Roster().setVisible(true);
    }
    //Action to apply holiday
	if (event.getSource() == holiConfirmJButton)
	{
	 
      Driver driver = Driver.createDriver(driverId);
	    String start = startDateTextField.getText();
   	  String end = endDateTextField.getText();
      
      boolean s = isValidDate(start);
      boolean e = isValidDate(end);
      
      if (s == false || e == false)
      {
        Object[] options = {"OK"};
          JOptionPane.showOptionDialog(null,"The date that you provided is invalid","Warning",
                                       JOptionPane.DEFAULT_OPTION, 
        		                       JOptionPane.WARNING_MESSAGE,null,options,options[0]);
      }
      
      else
      {
   	    HolidayRequest request = driver.createHolidayRequest(start, end, driverId);
   	    boolean valid=request.validate(driver.getHolidaysTaken());
	      if(valid == false)
	      {
		      Object[] options = {"OK"};
          JOptionPane.showOptionDialog(null,"Hoildays must > 1 & < 25","Warning",
                                       JOptionPane.DEFAULT_OPTION, 
        		                       JOptionPane.WARNING_MESSAGE,null,options,options[0]);
	      }
      
    
    
      else
      {
        stDateConfirmed.setText("" + request.getStartDay());
   	    edDateConfirmed.setText("" + request.getEndDay());
   	    noDaysConfirmed.setText("" + request.getNumOfDays());
        DriverHoliTakenA.setText("" + (driver.getHolidaysTaken() + request.getNumOfDays()));
        request.setDB(); 
      }
    }
  }         
	
	  // if days taken has been more than 25 already, a warning dialog will pop up
   	else if(DriverHoliTaken >= 25 && event.getSource() == holiApplyJButton)
    {
      Object[] options = {"OK"};
      JOptionPane.showOptionDialog(null,"Holiday required faild, 25 days already taken","Warning",
                                   JOptionPane.DEFAULT_OPTION, 
    		                       JOptionPane.WARNING_MESSAGE,null,options,options[0]);
    }
    else if(driverId == 0 && event.getSource() != computeJButton)
		{
			
      Object[] options = { "OK" };  
			JOptionPane.showOptionDialog(null,
					"Log in first, please", "Warning",
					JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE, null,
					options, options[0]);
		}
          
  }// actionPerformed
  
  public static int getDriverID()
  {
  	return driverId;
  }
  
  public boolean isValidDate(String date)
	{
	    // set date format dd/MM/yyyy
	    SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
	     
	    // testDate variable
	    Date testDate = null;
	 
	    // to parse the string into date form
	    try
	    {
       
	      testDate = sdf.parse(date);
        
	    }
	 
	    // if the format of the string provided doesn't match the format we
	    // declared in SimpleDateFormat() we will get an exception
	 
	    catch (ParseException e)
	    {
	       
	      return false;
	    }
	 
	    // dateformat.parse will accept any date as long as it's in the format
	    // you defined, it simply rolls dates over, for example, december 32
	    // becomes jan 1 and december 0 becomes november 30
	    // This statement will make sure that once the string
	    // has been checked for proper formatting that the date is still the
	    // date that was entered, if it's not, we assume that the date is invalid
	 
	    if (!sdf.format(testDate).equals(date))
	    {
	      return false;
	    }
	    else
	      return true;
	 
	} // end isValidDate



} // class GCD

   
